﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.Data.SqlClient;

namespace TextMIS
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        public static string userName;
        public static string userRight;
        public static string SQLstr = "server=wq-d248b39fdb10;uid=sa;pwd=123123;database=HospitalDB";

        SqlConnection conn;
  //      SqlCommand com;
        SqlDataAdapter da;
   //     SqlDataReader dr;
        


        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                timer1.Start();
                conn = new
             SqlConnection(SQLstr);
                DataSet ds = new DataSet();
                da = new SqlDataAdapter("select UserName from UserInfo", conn);
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                da.Fill(ds, "UserInfo");
                conn.Close();
                cbxUserName.DataSource = ds.Tables["UserName"];
                cbxUserName.DisplayMember = "UserName";
                this.cbxUserName.Items.Add("hb");

                this.cbxUserRight.Items.Add("普通用户");
                this.cbxUserRight.Items.Add("体检医生");
                this.cbxUserRight.Items.Add("系统管理员");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void btLogin_Click(object sender, EventArgs e)
        {
            try
            {
                int count = 0;
                da = new SqlDataAdapter("select * from UserInfo where UserName='" +cbxUserName.Text.Trim() + "' and UserPassword='" + txtUserPwd.Text.Trim() + "'and UserRight='"+ cbxUserRight.Text.Trim() +"'", conn);
                DataSet ds = new DataSet();
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                da.Fill(ds, "UserInfo");
                conn.Close();
                if (ds.Tables["UserInfo"].Rows.Count > 0)
                {
                    userName = cbxUserName.Text.ToString();
                    userRight = cbxUserRight.Text.ToString();
                    
                    this.Hide();
                    frmMain frmmain = new frmMain();
                    if (frmmain.ShowDialog() == DialogResult.No)
                    {
                        count--;
                        this.Show();
                        frmmain.Close();
                    }
                    if (count == 0)
                    {
                        frmmain.Show();
                    }
                    
                }
                else
                {
                    MessageBox.Show("用户名,密码或者权限错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtUserPwd.Text = "";
                    cbxUserRight.Text = "";
                    cbxUserName.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void btRegister_Click(object sender, EventArgs e)
        {
            frmRegister frmr = new frmRegister();
            this.Hide();


            if (frmr.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
            frmr.Show();
        }

        private void btEditPwd_Click(object sender, EventArgs e)
        {
            frmEditPwd frmep = new frmEditPwd();
            this.Hide();


            if (frmep.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
            frmep.Show();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //TimeSpan ts = new TimeSpan(Environment.TickCount * TimeSpan.TicksPerMillisecond);
            //string TempStr = string.Format("系统已经运行时间：{0:d2}天 {1:d2}时 {2:d2}分 {3:d2}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);

            DateTime dt = DateTime.Now;
            string TempStr2 = DateTime.Now.ToString("系统当前时间：yyyy/MM/dd HH:mm:ss");

            //labTime.Text = TempStr + "   " + TempStr2;
            labTime.Text = TempStr2;
        }
    }
}
